package com.zui.webapp.action;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.displaytag.pagination.PaginatedList;
import org.hibernate.criterion.DetachedCriteria;

import com.zui.entity.party.Person;
import com.zui.manager.PersonManager;
import com.zui.util.Page;

public class PersonAction extends BaseJQGridAction {

	private static final long serialVersionUID = 1L;

	private static final Log log = LogFactory.getLog(PersonAction.class);

	public PersonAction() {
	}	
	public PersonAction(PersonManager personManager) {
		this.personManager = personManager;
	}

	private List<Person> list;
	private Person person;

	// current page
	private Integer page = 0;
	// total page nums
	private Integer total = 0;
	// total record num
	private Integer records = 0;

	public void setRecords(Integer records) {
		this.records = records;
		if (this.records > 0 && jq.getPageSize() > 0) {
			this.total = (int) Math.ceil((double) this.records
					/ (double) jq.getPageSize());
		} else {
			this.total = 0;
		}
	}

	// fields without getter method are not serialized
	private PersonManager personManager;

	public void setPersonManager(PersonManager personManager) {
		this.personManager = personManager;
	}

	public String index() {
		// DetachedCriteria criteria = DetachedCriteria.forClass(Person.class);
		// // list = personManager.findByCriteria(criteria);
		// list = personManager.findByCriteria(criteria, dtPage == null ? new
		// Page()
		// : dtPage, null);

		log.debug("list() method in class PersonAction is executed!");
		return "index";
	}

	public String view() {
		Person person = (Person) personManager.findById(new Integer(1));
		log.debug("view() method in class PersonAction is executed!");
		return "view";
	}

	public String edit() {
		Person person = (Person) personManager.findById(new Integer(1));
		log.debug("edit() method in class PersonAction is executed!");
		return "edit";
	}

	public String editNew() {
		log.debug("editNew() method in class PersonAction is executed!");
		return "editNew";
	}

	public String create() {
		Person person = new Person();
		person.setFirstName("zuizui");
		person.setLastName("z");
		personManager.saveEntity(person);

		log.debug("create() method in class PersonAction is executed!");
		return "view";
	}

	public String delete() {
		log.debug("delete() method in class PersonAction is executed!");
		return "index";
	}

	public String jsonList() {
		DetachedCriteria criteria = DetachedCriteria.forClass(Person.class);
		if (jq.get_search()) {
			log.debug("defulat search launched for " + Person.class
					+ ",searchField:" + jq.getSearchField() + ",searchOper:"
					+ jq.getSearchOper() + ",searchString:"
					+ jq.getSearchString());
		}
		list = personManager.findByCriteria(criteria, jq == null ? new Page()
				: jq);
		this.setRecords(((PaginatedList) list).getFullListSize());
		log.debug("jsonList() method in class PersonAction is executed!");
		return "jsonList";
	}

	// public String getJSON() {
	// return jsonList();
	// }

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public List<Person> getList() {
		return list;
	}

	public void setList(List<Person> list) {
		this.list = list;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public Integer getRecords() {
		return records;
	}

}
